//
//  loginEventModelTableViewController.m
//  TRSAnalytics
//
//  Created by 824810056 on 2018/1/31.
//  Copyright © 2018年 Miridescent. All rights reserved.
//

#import "loginEventModelTableViewController.h"

#import "TRSAnalytics.h"
#import "TRSManager.h"
#define kTRSManager  [TRSManager sharedManager]

@interface loginEventModelTableViewController ()
@property (nonatomic, strong) NSMutableArray *titleArray;
@property (nonatomic, strong) NSMutableArray *detailArray;
@end

@implementation loginEventModelTableViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [TRSAnalytics pageBegin:@"2222"];
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    NSMutableDictionary *dic = [@{@"se_name": @"进入222页面"} mutableCopy];
    
    [TRSAnalytics pageEnd:@"2222" properties:dic];
   
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.titleArray = [@[@"userID", @"username", @"userArributes"] mutableCopy];
    self.detailArray = [@[@"1", @"2", @"3"] mutableCopy];
    
    [self.tableView reloadData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.titleArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *loginEventIdentifier = @"loginEventIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:loginEventIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:loginEventIdentifier];
    }
    cell.textLabel.text = self.titleArray[indexPath.row];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", self.detailArray[indexPath.row]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 100;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 100)];
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    loginBtn.frame = CGRectMake(25, 25, 80, 50);
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:loginBtn];
    
    UIButton *logout = [UIButton buttonWithType:UIButtonTypeSystem];
    logout.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2, 25, 80, 50);
    [logout setTitle:@"登出" forState:UIControlStateNormal];
    [logout addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:logout];
    
    UIButton *change = [UIButton buttonWithType:UIButtonTypeSystem];
    change.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2+40, 25, 80, 50);
    [change setTitle:@"修改" forState:UIControlStateNormal];
    [change addTarget:self action:@selector(change) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:change];
    
    return headView;
    
}
- (void)login{
    
    NSMutableDictionary *dic = [@{@"uid":@"1111",
                                  @"se_un":@"123123",
                                  @"aaa":@"123123123",
                                  @"bbb":@"123123"
                                  } mutableCopy];
    [TRSAnalytics login:dic];
    
    NSLog(@"uid == %@\nusername == %@ \nexterninfo == %@", [kTRSManager.defaultCacheManage deviceInfoWithKey:@"uid"], [kTRSManager.defaultCacheManage deviceInfoWithKey:@"se_un"], [kTRSManager.defaultCacheManage deviceInfoWithKey:@"extraInfo"]);
    
}
- (void)logout{
    [TRSAnalytics logout];
    NSLog(@"uid == %@\nusername == %@ \nexterninfo == %@", [kTRSManager.defaultCacheManage deviceInfoWithKey:@"uid"], [kTRSManager.defaultCacheManage deviceInfoWithKey:@"se_un"], [kTRSManager.defaultCacheManage deviceInfoWithKey:@"extraInfo"]);
}
- (void)change{
    NSMutableDictionary *dic = [@{@"uid":@"333",@"se_un":@"444"} mutableCopy];
    [TRSAnalytics modifyUserInfo:dic];
    NSLog(@"uid == %@\nusername == %@ \nexterninfo == %@", [kTRSManager.defaultCacheManage deviceInfoWithKey:@"uid"], [kTRSManager.defaultCacheManage deviceInfoWithKey:@"se_un"], [kTRSManager.defaultCacheManage deviceInfoWithKey:@"extraInfo"]);
}
@end
